using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace EzDeploy.Core.Design.Editor
{
    internal class DeployedDirectoryNameEditor : UITypeEditor
    {
        FolderBrowserDialog _openFolderDialog;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                IWindowsFormsEditorService windowsFormsEditorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (windowsFormsEditorService != null)
                {
                    if (this._openFolderDialog == null)
                    {
                        this._openFolderDialog = new FolderBrowserDialog();
                        this.InitializeDialog(this._openFolderDialog);
                    }
                    if (value is string)
                    {
                        this._openFolderDialog.SelectedPath = (string)value;
                    }
                    if (this._openFolderDialog.ShowDialog() == DialogResult.OK)
                    {
                        value = this._openFolderDialog.SelectedPath;
                    }
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        protected virtual void InitializeDialog(FolderBrowserDialog openFolderDialog)
        {
            openFolderDialog.Description = "Select folder for output.";
            openFolderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
        }

    }
}
